package com.example.graduate.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.graduate.dto.*;
import com.example.graduate.env.ResponseCodeEnum;
import com.example.graduate.response.DataResponse;
import com.example.graduate.service.OrderService;
import com.example.graduate.token.JudgeToken;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping(value = "/order", produces = MediaType.APPLICATION_JSON_VALUE)
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 获取主页面数据
     * @param getDataRequest
     * @return
     */
    @PostMapping("/getInfo")
    @JudgeToken
    public DataResponse getOrderInfo(@RequestBody GetDataRequest getDataRequest) {
        log.info("【获取主页面数据】获取主页面信息:{}", JSONObject.toJSONString(getDataRequest));
        if (StringUtils.isNullOrEmpty(getDataRequest.getUserId())) {
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.getInfo(getDataRequest);
    }

    /**
     * 确认接单
     */
    @PostMapping("/acceptOrder")
    @JudgeToken
    public DataResponse acceptOrder(@RequestBody AcceptOrderRequest acceptOrderRequest){
        log.info("【确认接单】确认接单:{}", JSON.toJSONString(acceptOrderRequest));
        if (Objects.isNull(acceptOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.acceptOrder(acceptOrderRequest);
    }

    /**
     *  取消订单
     */
    @PostMapping("/cancelOrder")
    @JudgeToken
    public DataResponse cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest){
        log.info("【取消接单】取消接单:{}", JSON.toJSONString(cancelOrderRequest));
        if (Objects.isNull(cancelOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.cancelOrder(cancelOrderRequest);
    }

    /**
     *  完成订单
     */
    @PostMapping("/completeOrder")
    @JudgeToken
    public DataResponse completeOrder(@RequestBody CompleteOrderRequest completeOrderRequest){
        log.info("【完成订单】完成订单:{}", JSON.toJSONString(completeOrderRequest));
        if (Objects.isNull(completeOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.completeOrder(completeOrderRequest);
    }

    /**
     * 获取用户正在接单数据
     */
    @PostMapping("/getAcceptOrder")
    @JudgeToken
    public DataResponse getAcceptOrder(@RequestBody GetAcceptOrderRequest getAcceptOrderRequest){
        log.info("【获取{}所接收订单】",JSON.toJSONString(getAcceptOrderRequest));
        if (Objects.isNull(getAcceptOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.getAcceptOrder(getAcceptOrderRequest);
    }

    /**
     * 发单接口
     */
    @PostMapping("/addOrder")
    @JudgeToken
    public DataResponse addOrder(@RequestBody AddOrderRequest addOrderRequest){
        log.info("【发单接口,发单者:{},请求参数:{}】",addOrderRequest.getUserId(),JSON.toJSONString(addOrderRequest));
        if (Objects.isNull(addOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.addOrder(addOrderRequest);
    }

    /**
     *  获取用户历史发单数据
     */
    @PostMapping("/getHistoryOrder")
    @JudgeToken
    public DataResponse getHistoryOrder(@RequestBody GetHistoryOrderRequest getHistoryOrderRequest){
        log.info("【获取历史订单信息接口,用户:{},请求参数:{}】",getHistoryOrderRequest.getUserId(),JSON.toJSONString(getHistoryOrderRequest));
        if (Objects.isNull(getHistoryOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.getHistoryOrder(getHistoryOrderRequest);
    }


    /**
     *  获取用户历史接单数据
     */
    @PostMapping("/getHistoryAcceptOrder")
    @JudgeToken
    public DataResponse getHistoryAcceptOrder(@RequestBody GetHistoryOrderRequest getHistoryOrderRequest){
        log.info("【获取历史接的订单信息接口,用户:{},请求参数:{}】",getHistoryOrderRequest.getUserId(),JSON.toJSONString(getHistoryOrderRequest));
        if (Objects.isNull(getHistoryOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        return orderService.getHistoryAcceptOrder(getHistoryOrderRequest);
    }

    /**
     * 获取用户正在发单数据
     */
    @PostMapping("/getPushOrder")
    @JudgeToken
    public DataResponse getPushOrder(@RequestBody GetPushOrderRequest getPushOrderRequest){
        log.info("【获取{}正在发送的订单】请求数据为：{}",getPushOrderRequest.getUserId(),JSONObject.toJSONString(getPushOrderRequest));
        if (Objects.isNull(getPushOrderRequest)){
            return new DataResponse(ResponseCodeEnum.PARAM_WRONG.getCode());
        }
        DataResponse pushOrder = orderService.getPushOrder(getPushOrderRequest);
        log.info("【获取{}正在发送的订单】响应数据为：{}", JSONObject.toJSONString(pushOrder));
        return pushOrder;
    }
}
